package com.tanhua.manage.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.tanhua.manage.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/system/users")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;
    @PostMapping("login")
    public ResponseEntity<Object> login(@RequestBody Map<String,String> body){
        System.out.println(body.get("username"));
        System.out.println(body.get("password"));
        System.out.println(body.get("verificationCode"));
        System.out.println(body.get("uuid"));
        return null;
    }
    @PostMapping("logout")
    public ResponseEntity<Object> logout(@RequestHeader("Authorization") String token){
        System.out.println(token);
        return null;
    }
    @PostMapping("profile")
    public ResponseEntity<Object> profile(@RequestHeader("Authorization") String token){
        System.out.println(token);
        return null;
    }
    /**
     * 生成验证码图片
     *
     * @param uuid
     */
    @GetMapping("verification")
    public void verification(@RequestParam("uuid") String uuid, HttpServletResponse response) throws IOException {
        response.setDateHeader("Expires", 0);
        // Set standard HTTP/1.1 no-cache headers.
        response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
        // Set IE extended HTTP/1.1 no-cache headers (use addHeader).
        response.addHeader("Cache-Control", "post-check=0, pre-check=0");
        // Set standard HTTP/1.0 no-cache header.
        response.setHeader("Pragma", "no-cache");
        response.setContentType("image/jpeg");

        // 生成验证码图片
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(299, 97);

        String code = lineCaptcha.getCode();

        log.info("生成验证码，uuid = " + uuid +", code = " + code);

        //TODO 业务操作

        // 输出验证码图片
        lineCaptcha.write(response.getOutputStream());
    }
}
